package com.longtengyuyun.reentrantLock;

import javax.sound.midi.Soundbank;
import javax.swing.text.DefaultCaret;
import java.lang.reflect.Proxy;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ZhaoYunLong
 * @date 2022/3/25
 * @description
 */
public class ReentrantLockCondition01 {

    ReentrantLock reentrantLock = new ReentrantLock();
    Condition condition = reentrantLock.newCondition();

    public static void main(String[] args) {
        ReentrantLockCondition01 reentrantLockCondition01 = new ReentrantLockCondition01();
        Consumer consumer = reentrantLockCondition01.new Consumer();
        Producer producer = reentrantLockCondition01.new Producer();


        consumer.start();
        producer.start();
    }


//    class Consumer extends Thread{
//
//        @Override
//        public void run() {
//            doConsumer();
//        }
//
//        private void doConsumer(){
//            try {
//                reentrantLock.lock();
//                System.out.println("consumer: " + Thread.currentThread());
//                condition.await();
//                System.out.println("consumer 等待了一段时间: " + Thread.currentThread());
//            } catch (Exception e) {
//                e.printStackTrace();
//            } finally {
//                reentrantLock.unlock();
//            }
//        }
//    }
//
//    class Producer extends Thread {
//
//        @Override
//        public void run() {
//           doProducer();
//        }
//
//        private void doProducer(){
//            try {
//                reentrantLock.lock();
//                System.out.println("producer: " + Thread.currentThread());
//                condition.signalAll();
//                System.out.println("信号  producer: " + Thread.currentThread());
//            } catch (Exception e) {
//                e.printStackTrace();
//            } finally {
//                reentrantLock.unlock();
//            }
//        }
//    }

    class Consumer extends Thread{

        @Override
        public void run() {
            consume();
        }

        private void consume() {

            try {
                reentrantLock.lock();
                System.out.println("我在等一个新信号"+Thread.currentThread().getName());
                condition.await();

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                System.out.println("拿到一个信号"+Thread.currentThread().getName());
                reentrantLock.unlock();
            }

        }
    }

    class Producer extends Thread{

        @Override
        public void run() {
            produce();
        }

        private void produce() {
            try {
                reentrantLock.lock();
                System.out.println("我拿到锁"+Thread.currentThread().getName());
                condition.signalAll();
//                System.out.println("我发出了一个信号："+Thread.currentThread().getName());
            } finally{
                System.out.println("我发出了一个信号："+Thread.currentThread().getName());
                reentrantLock.unlock();
            }
        }
    }


}
